{% if site.google_analytics %}
  <script>!function(w, d) {
    w.ga=w.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;

    /*{% if site.hydejack.cookies_banner %}*/
      if (navigator.CookiesOK) {
        ga('create', '{{ site.google_analytics }}', 'auto');
      } else if (d.cookie.indexOf("hy--cookies-ok=true") > -1) {
        ga('create', '{{ site.google_analytics }}', {
          'storage': 'none',
          'clientId': localStorage ? localStorage.getItem('ga--client-id') : undefined
        });
      } else {
        ga('create', '{{ site.google_analytics }}', {
          'storage': 'none'
        });
        ga('set', 'forceSSL', true);
        ga('set', 'anonymizeIp', true);
      }
    /*{% else %}*/
      ga('create', '{{ site.google_analytics }}', 'auto');
    /*{% endif %}*/

    var pushStateEl = d.getElementById('_pushState');
    var timeoutId;
    pushStateEl.addEventListener('hy-push-state-load', function() {
      w.clearTimeout(timeoutId);
      timeoutId = w.setTimeout(function() {
        ga('set', 'page', w.location.pathname);
        ga('send', 'pageview');
      }, 500);
    });

    d.addEventListener('hy--cookies-ok', function () {
      w.ga(function(tracker) {
        w.ga("set", "anonymizeIp", undefined);
        localStorage && localStorage.setItem("ga--client-id", tracker.get("clientId"));
      });
    });

    w.loadJSDeferred('https://www.google-analytics.com/analytics.js');
  }(window, document);</script>
{% endif %}
